package cn.com.backstage.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiT_Cash;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_shops_apply;
import cn.com.api.entity.ApiT_shops_pay_records;
import cn.com.api.entity.ApiT_stats;
import cn.com.api.entity.ApiT_user_admin;
import cn.com.api.entity.ApiT_user_role;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiT_shops_applyService;
import cn.com.api.service.ApiT_statsService;
import cn.com.api.service.ApiT_user_roleService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_shops_applyBaseController;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_shops_applyController  extends T_shops_applyBaseController {

	private static Logger logger = Logger.getLogger(T_shops_applyController.class);
	
	@Autowired
	private ApiT_shops_applyService apiT_shops_applyService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiShopsService shopsService;
	@Autowired
	private ApiT_statsService apiT_statsService;
	@Autowired
	private ApiT_user_roleService apiT_user_roleService;
	
	/**
	 * 进入列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "shopApplyList")
	public ModelAndView shopApplyList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_shops_apply_list.jsp");
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		Map<String,Object> map=new HashMap<String,Object>();
		String sname=request.getParameter("sname");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");
		map.put("sname", sname);
		map.put("stime", stime);
		map.put("etime", etime);
		list=apiT_shops_applyService.searAll(sname==null?"":sname,stime,etime);
		mv.addObject("apply", list);
		mv.addObject("tj",map);
		return mv;
	}
	
	/**
	 * 进入新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "shopApplyAddPre")
	public ModelAndView shop_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_shops_apply_add.jsp");
		String id=request.getParameter("id");
		Map<String,Object> apply=new HashMap<String,Object>();
		if(id!=null&&!"".equals(id)){
			apply=apiT_shops_applyService.searById(id);
			mv.addObject("apply",apply);
		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	}
	
	/**
	 * 新增申请
	 * 
	 * @return
	 */
	@RequestMapping(value = "shopApplyAdd")
	public void shopApplyAdd(ApiT_shops_apply apply,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		String money=request.getParameter("money");
		String remark=request.getParameter("remark");
		String status=request.getParameter("status");
		if(id!=null&&!"".equals(id)){
			if("2".equals(status)){
				apiT_shops_applyService.updateMoneyAndRemAndStatus(money, remark, "0", id);
			}else{
				apiT_shops_applyService.updateMoneyAndRem(money, remark, id);
			}
		}else{
			apiT_shops_applyService.addApply(apply);
		}
	}
	
	/**
	 * 审查结果
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateCheck")
	public void updateCheck(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		String lose=request.getParameter("lose");
		String status=request.getParameter("status");
		apiT_shops_applyService.updateCheck(lose, status, id);
	}
	
	/**
	 * 打款
	 * 
	 * @return
	 */
	@RequestMapping(value = "payMoney")
	public void payMoney(HttpServletRequest request,
			HttpServletResponse response,ApiT_shops_pay_records payRecord) throws Exception {
		String shopid=request.getParameter("shopid");
		String applyid=request.getParameter("applyid");
		String money=request.getParameter("money");
		DecimalFormat df = new DecimalFormat("######0.00");
		Map<String,Object> shop=new HashMap<String,Object>();
		shop=shopsService.findShopByShopId(shopid);
		String pre_payment=df.format(Double.parseDouble(shop.get("pre_payment")+"")+Double.parseDouble(money));
		shopsService.updatePreMoney(pre_payment, shopid,payRecord,applyid);
		//apiT_shops_applyService.updatePayMoney("1", applyid);
	}
	
	
	/**
	 * 待转账的列表(预付款申请)
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("transfer-list")
	public ModelAndView transferList(ApiT_shops_apply conditions,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/transfer_list.jsp");
		
		List<ApiT_shops_apply> transfers = apiT_shops_applyService.checkedTransferList(conditions, pageInfo);
		
		List<ApiT_city> cities = startOnLoadService.citys();
		
		mv.addObject("transfers", transfers);
		mv.addObject("conditions",conditions);
		mv.addObject("citys",cities);

		return mv;
	}
	
	/**
	 * 待转账的列表(提现申请)
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("cash-transfer-list")
	public ModelAndView cashTransferList(ApiT_Cash conditions,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/cash-transfer-list.jsp");
		List<ApiT_Cash> transfers = apiT_shops_applyService.checkedCashTransferList(conditions, pageInfo);
		List<ApiT_city> cities = startOnLoadService.citys();
		
		mv.addObject("transfers", transfers);
		mv.addObject("conditions",conditions);
		mv.addObject("citys",cities);

		return mv;
	}
	
	
	/**
	 * 商家待转账的列表(月结)
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("shop-month-shipping")
	public ModelAndView shopMonthShipping(ApiT_stats conditions,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shop-month-shipping.jsp");
		List<ApiT_stats> stats = apiT_statsService.shopMonthShippingList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions",conditions);
		return mv;
	}
	
	/**
	 * 商家待转账的列表(周结)
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("shop-week-shipping")
	public ModelAndView shopWeekShipping(ApiT_stats conditions,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shop-week-shipping.jsp");
		List<ApiT_stats> stats = apiT_statsService.shopWeekShippingList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions",conditions);
		return mv;
	}
	
	/**
	 * 分站待转账的列表(日结)
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("day-station-stat")
	public ModelAndView dayStationStat(ApiT_stats conditions,PageInfo pageInfo,HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/day-station-stat.jsp");
		List<ApiT_stats> stats = apiT_statsService.dayStationStatList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions",conditions);
		
		//查看登陆管理员的角色
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
		ApiT_user_role stationRole = apiT_user_roleService.getStationRole();
		if (stationRole != null){//存在站长角色
			if (admin.getRole_id().intValue() == stationRole.getRole_id().intValue()){//登陆的管理员是站长
				mv.addObject("isStation", true);
			}else{
				mv.addObject("isStation", false);
			}
		}
		
		return mv;
	}
	
	/**
	 * 请求打款给站长（按日）
	 * @param stats_id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("re-pay-money-to-station")
	public ModelAndView rePayMoneyToStation(Integer stats_id,int type) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/pay-money-to-station.jsp");
		ApiT_stats stat = apiT_statsService.getStationStatById(stats_id);
		mv.addObject("stat", stat);
		mv.addObject("type", type);
		return mv;
	}
	
	
	
	/**
	 * 请求打款给商家（按周）
	 * @param stats_id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("re-pay-week-money-to-shop")
	public ModelAndView rePayWeekMoneyToShop(Integer stats_id) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/pay-week-money-to-shop.jsp");
		ApiT_stats stat = apiT_statsService.getShopWeekStatById(stats_id);
		ApiShop shop = shopsService.findShopByCid(stat.getShop_id()+"");
		mv.addObject("stat", stat);
		mv.addObject("shop", shop);
		return mv;
	}
	
	/**
	 * 确认打款给商家（按周）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("pay-week-money-to-shop")
	public @ResponseBody Map<String,String> payWeekMoneyToShop(Integer stats_id,ApiT_shops_pay_records payRecord) throws Exception {
		Map<String,String> result = apiT_statsService.payWeekMonyToShop(stats_id,payRecord);
		return result;
	}
	
	
	
	/**
	 * 请求打款给商家（按月）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("re-pay-month-money-to-shop")
	public ModelAndView rePayMonthMoneyToShop(Integer stats_id) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/pay-month-money-to-shop.jsp");
		ApiT_stats stat = apiT_statsService.getShopMonthStatById(stats_id);
		ApiShop shop = shopsService.findShopByCid(stat.getShop_id()+"");
		mv.addObject("stat", stat);
		mv.addObject("shop", shop);
		return mv;
	}
	
	/**
	 * 确认打款给商家（按月）
	 * @param conditions 不同搜索条件
	 * @param pageInfo 分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("pay-month-money-to-shop")
	public @ResponseBody Map<String,String> payMonthMoneyToShop(Integer stats_id,ApiT_shops_pay_records payRecord) throws Exception {
		Map<String,String> result = apiT_statsService.payMonthMonyToShop(stats_id,payRecord);
		return result;
	}
	
	
	/**
	 * 打款
	 * @return
	 */
	@RequestMapping("payCashMoney")
	@ResponseBody
	public Map<String,Object> payCashMoney(HttpServletRequest request,ApiT_Cash cash,ApiT_shops_pay_records payRecord) {
		Map<String,Object> result = new HashMap<String,Object>();
		try{
			int count = apiT_shops_applyService.updatCashPay(cash.getId(),cash.getShopid(), cash.getMoney(),payRecord);
			if(count > 0){
				result.put("0000", "更新成功");
			}else{
				result.put("-1", "没有记录被更新");
			}
		}catch(Exception ex){
			result.put("-1", "发生异常");
		}
		return result;
	}
	
}



